package com.huitone.smspfm.core.filter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huitone.smspfm.logger.ExtLogger;
import org.springframework.data.redis.connection.RedisConnection;

import com.bin.common.utils.DateTimeUtils;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.core.config.AppStaticConfigs;
import com.huitone.smspfm.core.constant.DealResult;
import com.huitone.smspfm.redis.model.AppCanstant;
import com.huitone.smspfm.redis.model.SmsEntity;
import com.huitone.smspfm.redis.pipeline.PipelineOpts.ABExcuteCommands;

/**   
* @Title IlligalUserFilterTask.java 
* @Package com.huitone.smspfm.core.task 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年6月17日 上午11:32:00 
* @version V1.0   
*/
public class IlligalUserFilterTask2 extends Thread {
	
	private ExtLogger logger = ExtLogger.create(IlligalUserFilterTask2.class);
	
	private String moduleName = "非法用户过滤模块";
	
	private byte[] moduleNameB = AppStaticConfigs.stringRedisSerializer.serialize(moduleName);
	
	private List<SmsEntity>[] results = null;
	
	private List<SmsEntity> entities = null;
	
	private int index = 0;
	
	private Map<String, Integer> filters = null;
	
	public IlligalUserFilterTask2(List<SmsEntity> entities, List<SmsEntity>[] results, int index) {
		this.entities = entities;
		this.results = results;
		this.index = index;
	}
	
	public IlligalUserFilterTask2(List<SmsEntity> entities, List<SmsEntity>[] results, int index, Map<String, Integer> filters) {
		this.entities = entities;
		this.results = results;
		this.index = index;
		this.filters = filters;
	}

	@Override
	public void run() {
		
		if (entities == null) return;
		
		long start = System.currentTimeMillis();
		
		AppStaticConfigs.pipelineOpts.save(new ABExcuteCommands() {

			@Override
			public void excute(RedisConnection connection) {
				
				List<SmsEntity> list = new ArrayList<>();
				
				for (SmsEntity entity:entities) {
					
					Map<byte[], byte[]> hashMap = new HashMap<>();
					hashMap.put(AppCanstant.SmsEntity.DealModuleName, moduleNameB);
					hashMap.put(AppCanstant.SmsEntity.SdealTime, AppStaticConfigs.stringRedisSerializer.serialize(DateTimeUtils.dateToStr2(new Date())));
					hashMap.put(AppCanstant.SmsEntity.ConSm, AppStaticConfigs.stringRedisSerializer.serialize(StringUtils.toString(StringUtils.strToInt(entity.getConSm()))));
					
					String seqNum = entity.getSequence() + ":" + entity.getUser();
					String smInfo = AppStaticConfigs.pSMCacheHash + ":" + seqNum;
					
					if (!StringUtils.isPhNum(entity.getUser())) {
						
						Date endTime = new Date();
						
						hashMap.put(AppCanstant.SmsEntity.EdealTime, AppStaticConfigs.stringRedisSerializer.serialize(DateTimeUtils.dateToStr2(endTime)));
						
						connection.hMSet(AppStaticConfigs.stringRedisSerializer.serialize(smInfo), hashMap);
						
						
						filters.put(seqNum, DealResult.RESULT_6);
						
						logger.warn("[序列号:用户号码][" + entity.getSequence() + ":" + entity.getUser() + "]被不合法号码检测过滤器捕获,处理流程结束。");
						
					} else {
						
						Date endTime = new Date();
						
						hashMap.put(AppCanstant.SmsEntity.EdealTime, AppStaticConfigs.stringRedisSerializer.serialize(DateTimeUtils.dateToStr2(endTime)));
						hashMap.put(AppCanstant.SmsEntity.DealResult, DealResult.RESULT0B);
						hashMap.put(AppCanstant.SmsEntity.DealResultDesc, DealResult.RESULT0_DESCB);
						
						connection.hMSet(AppStaticConfigs.stringRedisSerializer.serialize(smInfo), hashMap);
						
						list.add(entity);
					}
				}
				
				results[index] = list;
			}
		});
		
		long end = System.currentTimeMillis();
		
		logger.debug("非法用户过滤:" + (end - start) + "毫秒");
	}
}
